System = typeof(System) != "undefined" && System || window.top.System || new function()
{
	this.WebMode = true;
	
	this.Gadget = new function()
	{
		this.settingsUI = null;
		this.onSettingsShow = null;
		this.onSettingsClosed = null;
		this.onSettingsClosing = null;
		this.document = window.top.document;
		this.window = window.top;
		
		this.Settings = new function()
		{
			var localStorage = {};
			var delayedWrite = null;
			var settingsButtons = null;
				
			var action = 
			{
				cancel: 0,
				commit: 1
			}
			var closeAction = action.cancel;
			
			function escape(str)
			{
   				return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
			}
			
			function setCookie(key, value, ttlDays)
			{
				if (ttlDays == null)
					ttlDays = 7;
				
				var d = new Date();
				d.setTime(d.getTime() + (7*24*60*60*1000));
				
				var expires = "expires=" + d.toGMTString();
				document.cookie = key + "=" + value + "; " + expires;
			}
			
			function getCookie(key)
			{
				var cookies = document.cookie.split(";");
				for (var i = 0, length = cookies.length; i < length; i++)
				{
					var value = cookies[i];
					var j = value.indexOf("=");
					
					if (j != -1)
					{
						var k = value.substr(0, j);
						k = (k.trim ? k.trim() : k.replace(/^\s+|\s+$/gm, ''));
						value = value.substr(j + 1);
						
						if (k == key)
							return value;
					}
				}
				
				return null;
			}

			function load()
			{
				var storage = null;
				try
				{
					storage = eval("(" + decodeURIComponent(getCookie("storage")) + ")");
				}
				catch(ex) 
				{
					console.error(ex.message);
				}
				
				if (storage)
					localStorage = storage;
				else
					localStorage = {};
			}
			load();
			
			function save()
			{
				var json = "{";
				var i = 0;
				
				for (var key in localStorage)
				{
					var value = localStorage[key];
					switch(typeof(value))
					{
						case "number":
						case "boolean":
							break;
						case "object":
							if (value == null)
								break;
							else
							{
								//not supported
								console.error("Writing objects is not supported");
							}
						default:
							value = "\"" + escape(value) + "\"";
					}
					if (i++ > 0)
						json += ",";
					json += "\"" + key + "\":" + value;
				}
				
				json += "}";
				
				setCookie("storage", json);
			}
			
			this.read = function(key)
			{
				var o = localStorage[key];
				if (o == null)
					return "";
				return localStorage[key];
			}
			
			this.write = function(key, value)
			{
				localStorage[key] = value;
				
				if (delayedWrite)
					clearTimeout(delayedWrite)
				delayedWrite = setTimeout(
					function()
					{
						delayedWrite = null;
						save();
					}, 100);
			}
			
			this.show = function()
			{
				System.Gadget.Flyout.file = System.Gadget.settingsUI;
				System.Gadget.Flyout.onShow = function(frame)
				{
					closeAction = action.cancel;
					
					if (frame)
					{
						frame.style.left = "20px";
						frame.style.top = "20px";
					}
					
					if (!settingsButtons)
					{
						settingsButtons = document.createElement("div");
						settingsButtons.style.position = "absolute";
						settingsButtons.style.left = "10px";
						settingsButtons.style.top = "10px";
						settingsButtons.style.width = (frame.offsetWidth + 20) + "px";
						settingsButtons.style.height = (frame.offsetHeight + 20 + 40) + "px";
						settingsButtons.style.backgroundColor = "#f0f0f0";
						settingsButtons.style.zIndex = 1001;
						
						var line = document.createElement("div");
						line.style.position = "absolute";
						line.style.left = "0px";
						line.style.bottom = "40px";
						line.style.width = "100%";
						line.style.height = "1px";
						line.style.backgroundColor = "#ccc";
						settingsButtons.appendChild(line);
						
						var buttonOk = document.createElement("input");
						buttonOk.type = "button";
						buttonOk.value = "OK";
						buttonOk.style.width = "70px";
						buttonOk.style.height = "20px";
						buttonOk.style.position = "absolute";
						buttonOk.style.right = "90px";
						buttonOk.style.bottom = "10px";
						buttonOk.style.fontSize = "10px";
						settingsButtons.appendChild(buttonOk);
						
						var buttonCancel = document.createElement("input");
						buttonCancel.type = "button";
						buttonCancel.value = "Cancel";
						buttonCancel.style.width = "70px";
						buttonCancel.style.height = "20px";
						buttonCancel.style.position = "absolute";
						buttonCancel.style.right = "10px";
						buttonCancel.style.bottom = "10px";
						buttonCancel.style.fontSize = "10px";
						settingsButtons.appendChild(buttonCancel);
						
						buttonOk.onclick = function()
						{
							closeAction = action.commit;
							System.Gadget.Flyout.setVisibility(false);
						}
						
						buttonCancel.onclick = function()
						{
							closeAction = action.cancel;
							System.Gadget.Flyout.setVisibility(false);
						}
						
						document.body.appendChild(settingsButtons);
					}
					
					settingsButtons.style.display = "block";	
						
					if (System.Gadget.onSettingsShow)
						System.Gadget.onSettingsShow();
				}
				
				System.Gadget.Flyout.onClosed = function()
				{
					if (settingsButtons)
						settingsButtons.style.display = "none";
					
					if (System.Gadget.onSettingsClosed)
					{
						var event = 
						{
							closeAction: closeAction,
							Action: action
						}
						
						System.Gadget.onSettingsClosed(event);
					}
				}
				
				System.Gadget.Flyout.onClosing = function()
				{
					if (System.Gadget.onSettingsClosing)
					{
						var event = 
						{
							closeAction: closeAction,
							Action: action
						}
						
						System.Gadget.onSettingsClosing(event);
					}
				}				
				
				System.Gadget.Flyout.setVisibility(true);
			}
		}
		
		this.Flyout = new function()
		{
			var base = this;
			var frame = null;
			var overlay = null;
			
			this.show = false;
			this.onShow = null;
			this.onClosed = null;
			this.onClosing = null;
			this.document = null;
			this.window = null;
			this.file = null;
			
			this.setVisibility = function(visible)
			{
				this.show = visible;
				
				if (visible)
				{
					if (!frame)
					{
						overlay = document.createElement("div");
						frame = document.createElement("iframe");
						frame.src = "about:blank";
						
						frame.onload = function()
						{
							if (frame.src == "about:blank")
								return;
								
							frame.style.left = document.body.offsetWidth + "px";
							frame.style.width = frame.contentDocument.body.offsetWidth + "px";
							frame.style.height = frame.contentDocument.body.offsetHeight + "px";
							frame.style.visibility = "visible";
							
							base.document = frame.contentDocument || frame.contentWindow.document;
							base.window = frame.contentWindow;
							if (base.onShow)
								base.onShow(frame);
						}
						
						frame.style.visibility = "hidden";
						frame.style.display = "block";
						frame.style.position = "absolute";
						frame.style.top = "0px";
						frame.style.border = "0px"
						frame.style.zIndex = 1010;
						
						overlay.style.position = "absolute";
						overlay.style.top = "0px";
						overlay.style.left = "0px";
						overlay.style.width = "100%";
						overlay.style.height = "100%";
						overlay.style.backgroundColor = "#000";
						overlay.style.opacity = "0.7";
						overlay.style.filter = "alpha(opacity=70)";
						overlay.style.zIndex = 1000;
						
						overlay.onclick = function()
						{
							base.setVisibility(false);
						}
						
						document.body.appendChild(frame);
						document.body.appendChild(overlay);
					}
					
					overlay.style.display = "block";
						
					if (true || frame.file != base.file) //forcing reloads
					{
						frame.style.visibility = "hidden";
						frame.style.display = "block";
						frame.src = base.file;
						frame.file = base.file;
					}
					else
					{
						frame.style.display = "block";
						base.document = frame.contentDocument || frame.contentWindow.document;
						base.window = frame.contentWindow;
						if (base.onShow)
							base.onShow();
					}
				}
				else if (frame)
				{
					if (base.onClosing)
						base.onClosing();
						
					frame.style.display = "none";
					overlay.style.display = "none";
					frame.src = "about:blank";
					base.document = null;
					base.window = null;
					
					if (base.onClosed)
						base.onClosed();
						
					base.onShow = null;
					base.onClosed = null;
					base.onClosing = null;
				}
			}
		}
	}
	
	this.Sound = new function()
	{
		var audio = null;
		
		this.playSound = function(path)
		{
			if (!path)
				return;
				
			if (!Audio)
			{
				audio = document.createElement("object");
				audio.data = path;
			}
			else
			{
				if (audio instanceof Audio)
					audio.pause();
				audio = new Audio(path);
				audio.volume = 0.8;
				audio.play();
			}
		}
	}
}

function IsWebMode()
{
	try
	{
		return !!System.WebMode;		
	}
	catch (ex)
	{
	}
	return false;
}